package dk.controller;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ReturnData.ResponseData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import dk.Service.TokenService;
import dk.Utils.SendSms;
import dk.beans.Examlist;
import dk.beans.Student;
import dk.beans.studentexamlist;
import dk.mapper.StudentMapper;

@RestController
public class StudentController {
	@Autowired
	StudentMapper studentmapper;
	@Autowired
	TokenService tokenservice;
//增删改查 
	//普通查询
	@PostMapping("GetStudentListByPage")
	public ResponseData GetStudentListByPage(Integer pagenum,Integer count,String stuname) {
		ResponseData data = new ResponseData();
		IPage<Student> page = new Page<Student>(pagenum,count);
		page = studentmapper.selectPage(page, new QueryWrapper<Student>().like(StringUtils.hasText(stuname), "username", stuname));
		//List<Student> list = page.getRecords();
		data.setCode(200);
		data.setMessage("分页查询学生列表成功");
		data.setData(page);	
		return data;
	}
	//根据id删除学生
	@PostMapping("DelStudentListById")
	public ResponseData DelStudentListById(Integer id) {
		ResponseData data = new ResponseData();
		studentmapper.delete(new QueryWrapper<Student>().eq("id",id));
		data.setMessage("根据id删除Studentlist成功");
		data.setCode(200);
		return data;
	}
	//根据id得到学生信息
	@PostMapping("GetStudentListById")
    public ResponseData GetStudentListById(Integer id) {
		ResponseData data = new ResponseData();
		Student student = studentmapper.selectOne(new QueryWrapper<Student>().eq("id",id));
		data.setCode(200);
		data.setMessage("根据id查询StudentList成功");
		data.setData(student);
		return data;
	}
	//根据id提交对学生列表的修改
	@PostMapping("EditStudentListById")
	public ResponseData EditStudentListById(@RequestBody String studentstr) {
		String str = studentstr;
		
		str = StringEscapeUtils.unescapeJava(str);
		str = str.substring(30,str.length()-3);
		System.out.println(str);
		JSONObject jsonObject = JSONObject.parseObject(str);
		
		Student student= JSON.toJavaObject(jsonObject, Student.class);
		
		ResponseData data = new ResponseData();
		student.setLastlogintime(new Date());
		
		//查询名称是否重复
		Integer count = studentmapper.selectCount(new QueryWrapper<Student>().eq("username",student.getUsername()));
		if(count>0) {
			//数据库中有 查询id是否相同
			Student dbstudent = studentmapper.selectOne(new QueryWrapper<Student>().eq("username",student.getUsername()));
			if(dbstudent.getId() != student.getId()) {
				data.setMessage("用户名重复");
				data.setCode(201);
				return data;
			}
		}
		studentmapper.update(student, new QueryWrapper<Student>());
		data.setCode(200);
		data.setMessage("根据id编辑Student成功");
		
		data.setData(student);
		return data;
		
	}
	//根据id锁定学生
	@PostMapping("LockStudentById")
	public ResponseData LockStudentByid(Integer id) {
		ResponseData data = new ResponseData();
		Student student = studentmapper.selectOne(new QueryWrapper<Student>().eq("id", id));
		student.setIslocked(1);
		studentmapper.update(student, new QueryWrapper<Student>().eq("id", id));
		data.setCode(200);
		data.setMessage("修改状态为锁定成功");
		return data;
		
		
	}
	//根据id解锁学生
	@PostMapping("UnlockStudentBtId")
	public ResponseData UnlockStudentBtId(Integer id) {
		ResponseData data = new ResponseData();
		Student student = studentmapper.selectOne(new QueryWrapper<Student>().eq("id", id));
		student.setIslocked(0);
		studentmapper.update(student, new QueryWrapper<Student>().eq("id", id));
		data.setCode(200);
		data.setMessage("修改状态为解锁成功");
		return data;
	}
	//学生登录界面
	@PostMapping("StudentLogin")
	public ResponseData StudentLogin(String username,String password) {
		ResponseData data= new ResponseData();
		QueryWrapper<Student> qw = new QueryWrapper<Student>();
		qw.eq("usesrname",username);
		if(studentmapper.selectCount(qw) == 0) {
			data.setCode(201);
			data.setMessage("用户名不存在");
			return data;
		}
		else {
			
			Student student = studentmapper.selectOne(qw);
			if(student.getPassword().equals(password)) {
				data.setCode(200);
				data.setMessage("登录成功");
				return data;
			}
			else {
				data.setCode(201);
				data.setMessage("密码错误");
				return data;
			}
		}
		
	}
	//学生修改密码
	@PostMapping("ChangePassWordByid")
	public ResponseData ChangePassWordByid(String newpassword,String oldpassword,Integer id) {
		ResponseData data = new ResponseData();
		
		Student student = studentmapper.selectOne(new QueryWrapper<Student>().eq("id", id));
		if(student.getPassword().equals(oldpassword)) {
			//提供了旧密码 符合条件
			student.setPassword(newpassword);
			studentmapper.update(student, new QueryWrapper<Student>().eq("id", id));
	        data.setCode(201);
	        data.setMessage("密码修改失败");
	        return data;
		}
		else {
			data.setCode(200);
			data.setMessage("密码修改成功");
			return data;
			
		}
	}
	//学生发送验证码
	@PostMapping("SendMes")
	public ResponseData SendMes(String phone) {
		System.out.println("--------------------------");
       JSONObject jsonObject = JSONObject.parseObject(phone);
		
		phone = JSON.toJavaObject(jsonObject, String.class);
  	   
		ResponseData data = new ResponseData();
		Random random = new Random();
		
		int code = random.nextInt(8999);  
        code = code+1000; 
		SendSms send = new SendSms(phone, String.valueOf(code));
		send.send();
		System.out.println(code);
		String uuid = tokenservice.GetCodeUUidByCode(String.valueOf(code));
		data.setCode(200);
		data.setMessage("验证码发送成功");
		data.setData(uuid);
		return data;
	}
	
	
}
